package com.david.core.utils

import android.widget.ImageView
import androidx.appcompat.widget.AppCompatImageView
import com.bumptech.glide.Glide
import com.bumptech.glide.load.resource.bitmap.CenterCrop
import com.bumptech.glide.load.resource.bitmap.GranularRoundedCorners
import com.bumptech.glide.load.resource.bitmap.RoundedCorners
import com.bumptech.glide.request.RequestOptions
import com.david.core.R

/**
 * 使用阿里云/腾讯云服务时建议在链接后加参数来加载webp格式图片:参考链接：https://help.aliyun.com/zh/oss/user-guide/convert-image-formats
 */
fun ImageView.load(url: String? = null, holderRes: Int = R.mipmap.ic_img_placeholder, errorRes: Int? = null) {
    Glide.with(context).load(url).placeholder(holderRes).error(errorRes).into(this)
}

/**
 * 建议使用阿里云/腾讯云的链接后加参数修改为圆形图片的方法
 */
fun ImageView.loadCircleImg(url: String? = null, holderRes: Int = R.mipmap.ic_img_placeholder, errorRes: Int? = null) {
    val options = RequestOptions.circleCropTransform()
    Glide.with(context)
        .load(url)
        .placeholder(holderRes)
        .error(errorRes)
        .apply(options)
        //.diskCacheStrategy(DiskCacheStrategy.RESOURCE)
        .into(this)
}

fun ImageView.loadRoundCornerImg(url: String, radius: Int, holderRes: Int = R.mipmap.ic_img_placeholder, errorRes: Int? = null) {
    Glide.with(context)
        .load(url)
        .transform(CenterCrop(), RoundedCorners(radius)) // 16代表16px的圆角
        .placeholder(holderRes)
        .error(errorRes)
        //.diskCacheStrategy(DiskCacheStrategy.RESOURCE)
        .into(this)
}

fun ImageView.loadRoundCornerImg(url: String, topLeftR: Float = 0f, topRightR: Float = 0f, bottomRightR: Float = 0f, bottomLeftR: Float = 0f, holderRes: Int = R.mipmap.ic_img_placeholder, errorRes: Int? = null) {
    Glide.with(context)
        .load(url)
        .transform(CenterCrop(), GranularRoundedCorners(topLeftR, topRightR, bottomRightR, bottomLeftR)) // 16代表16px的圆角
        .placeholder(holderRes)
        .error(errorRes)
        //.diskCacheStrategy(DiskCacheStrategy.RESOURCE)
        .into(this)
}

fun AppCompatImageView.load(url: String? = null, holderRes: Int = R.mipmap.ic_img_placeholder, errorRes: Int? = null) {
    Glide.with(context).load(url).placeholder(holderRes).error(errorRes).into(this)
}

/**
 * 建议使用阿里云/腾讯云的链接后加参数修改为圆形图片的方法
 */
fun AppCompatImageView.loadCircleImg(url: String? = null, holderRes: Int = R.mipmap.ic_img_placeholder, errorRes: Int? = null) {
    val options = RequestOptions.circleCropTransform()
    Glide.with(context)
        .load(url)
        .placeholder(holderRes)
        .error(errorRes)
        .apply(options)
        //.diskCacheStrategy(DiskCacheStrategy.RESOURCE)
        .into(this)
}

fun AppCompatImageView.loadRoundCornerImg(url: String, radius: Int, holderRes: Int = R.mipmap.ic_img_placeholder, errorRes: Int? = null) {
    Glide.with(context)
        .load(url)
        .transform(CenterCrop(), RoundedCorners(radius)) // 16代表16px的圆角
        .placeholder(holderRes)
        .error(errorRes)
        //.diskCacheStrategy(DiskCacheStrategy.RESOURCE)
        .into(this)
}

fun AppCompatImageView.loadRoundCornerImg(url: String, topLeftR: Float = 0f, topRightR: Float = 0f, bottomRightR: Float = 0f, bottomLeftR: Float = 0f, holderRes: Int = R.mipmap.ic_img_placeholder, errorRes: Int? = null) {
    Glide.with(context)
        .load(url)
        .transform(CenterCrop(), GranularRoundedCorners(topLeftR, topRightR, bottomRightR, bottomLeftR)) // 16代表16px的圆角
        .placeholder(holderRes)
        .error(errorRes)
        //.diskCacheStrategy(DiskCacheStrategy.RESOURCE)
        .into(this)
}
